<template>
  <div>
    <el-table :data="tableData" height="250" border style="width: 100%">
      <el-table-column prop="name" label="名称" width="100"> </el-table-column>
      <el-table-column label="单价" width="80" prop="price">
        <template slot-scope="scope">
          <span> {{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" width="280">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.num"
            @change="handleChange"
            :min="1"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="小计" width="100" prop="add">
        <template slot-scope="scope">
          <span> {{ scope.row.price * scope.row.num }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div>
      总价：<span>{{ total }}</span>
    </div>
    <button @click="jumpThree" class="jumpThree">跳转到作业三</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0,
      tableData: [
        {
          num: 1,
          id: 1,
          name: "apple",
          price: "3",
        },
        {
          num: 1,
          id: 2,
          name: "pear",
          price: "4",
        },
        {
          num: 1,
          id: 3,
          name: "banana",
          price: "5",
        },
      ],
    };
  },
  methods: {
    jumpThree() {
      this.$router.push("/workthree");
    },
    handleChange(value) {
      this.total = 0;
      this.tableData.forEach((item) => {
        this.total += item.price * item.num;
      });
    },
  },
  mounted() {
    this.tableData.map((item) => {
      this.total += item.price * item.num;
    });
  },
};
</script>

<style lang="sass" scoped>
</style>